package com.inossem.warehousestorage_inossem.activity.change_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageConfirmBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.ChangeStorageConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.library.smoothCheckBox.SmoothCheckBox;
import com.zhy.android.percent.support.PercentRelativeLayout;

public class ChangeStorageConfirmActivity extends BaseActivity {

    private static final String SUCCESS = "1";//成功

    private TextView title, confirm, creator, totalLine, submitLine, voucherDate, postDate, isFocusTextView;

    private PercentRelativeLayout dateLayout;

    private SmoothCheckBox isFocus;

    private PercentRelativeLayout submitOverviewLayout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_storage_confirm);
        initView(ChangeStorageConfirmActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ChangeStorageConfirmActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        creator = (TextView) activity.findViewById(R.id.creator);
        totalLine = (TextView) activity.findViewById(R.id.totalLine);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        dateLayout = (PercentRelativeLayout) activity.findViewById(R.id.dateLayout);
        voucherDate = (TextView) activity.findViewById(R.id.voucherDate);
        postDate = (TextView) activity.findViewById(R.id.postDate);
        isFocusTextView = (TextView) activity.findViewById(R.id.isFocusTextView);
        isFocus = (SmoothCheckBox) activity.findViewById(R.id.isFocus);
        submitOverviewLayout = (PercentRelativeLayout) activity.findViewById(R.id.submitOverviewLayout);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_change_storage_confirm_submit);
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                confirm(activity, voucherDate.getText().toString(), postDate.getText().toString(), isFocus.isChecked());
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        isFocusTextView.setVisibility(View.GONE);
        isFocus.setVisibility(View.GONE);
        isFocus.setOnCheckedChangeListener((SmoothCheckBox checkBox, boolean isChecked) -> {
            if (isChecked) {
                dateLayout.setVisibility(View.VISIBLE);
                voucherDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                postDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                voucherDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", voucherDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            voucherDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "voucherDate");
                    }
                });

                postDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", postDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            postDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "postDate");
                    }
                });
            } else {
                dateLayout.setVisibility(View.GONE);
                voucherDate.setText("");
                postDate.setText("");
            }
        });

        getData(activity);
    }

    private void confirm(Context context, String voucherDate, String postDate, boolean isFocus) {
        ChangeStorageConfirmRequestBean params = ChangeStorageUtils.getChangeStorageConfirmRequestBean(context, voucherDate, postDate, isFocus);
//        Gson gson = new Gson();
//        LogUtils.i("ChangeStorageConfirmRequestBean === " + gson.toJson(params , ChangeStorageConfirmRequestBean.class));
        ChangeStorageConfirmRequest.request(context, params, new GsonBeanListener<ChangeStorageConfirmBean>() {
            @Override
            public void onSuccess(ChangeStorageConfirmBean bean) {

                    SPUtils.removeChangeStorageInformation(context);
                    Intent intent = new Intent(context, ChangeStorageResultActivity.class);
                    intent.putExtra(OutStorageExtra.MESSAGE, bean.getHead().getMsg());
                    intent.putExtra(OutStorageExtra.CONFIRM_MESSAGE, "过账成功");
                    startActivity(intent);
//                } else {
//                    if (!TextUtils.isEmpty(bean.getData().getDumpNumber())) {
//                        ChangeStorageUtils.updateDumpNumber(context, bean.getData().getDumpNumber());
//                    }
//                    Utils.middleToast(context, bean.getData().getMsg());
//                }
            }

            @Override
            public void onFailing(ChangeStorageConfirmBean bean) {
                if (!TextUtils.isEmpty(bean.getBody())) {
                        ChangeStorageUtils.updateDumpNumber(context, bean.getBody());
                    }
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
            }
        });
    }


    private void getData(Context context) {
        ChangeStorageBean data = ChangeStorageUtils.getChangeStorageBean(context);
        creator.setText(SPUtils.getName(context));
        totalLine.setText(data.getMaterielList().size() + "");
        submitLine.setText(ChangeStorageUtils.getChangeStorageMaterielNumber(context) + "");
        submitOverviewLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context, ChangeStorageUtils.getOverViewBeanWithChangeStorageList(data.getMaterielList())).show();
            }
        });
    }

}
